package com.niiwoo.civet.trade.dao.mapper;

import com.niiwoo.civet.trade.dao.entity.ProjectBorrowerPlan;
import com.niiwoo.civet.trade.dto.ProjectBorrowerAmountSumDTO;
import com.niiwoo.civet.trade.dto.ProjectSummaryAmountDTO;
import com.niiwoo.civet.trade.dto.ToRepayTotalAmountDTO;
import com.niiwoo.civet.trade.dto.common.CurrMonthRefundPlanDTO;
import com.niiwoo.civet.trade.dto.common.DelinquencyDTO;
import com.niiwoo.civet.trade.dto.common.ProjectBorrowerPlanDTO;
import com.niiwoo.civet.trade.dto.request.ProjectBorrowerPlanRequestDTO;
import com.niiwoo.civet.trade.dto.response.CurrentPeriodToReturnDTO;
import com.niiwoo.civet.trade.dto.response.myloan.LoanGoingResponseDTO;
import com.niiwoo.civet.trade.dto.response.myloan.LoanPlanDateResponseDTO;
import com.niiwoo.civet.trade.dto.response.myloan.LoanPlanMonthResponseDTO;
import com.niiwoo.civet.trade.dto.response.myloan.ProjectrePaymentResponseDTO;
import org.apache.ibatis.annotations.Param;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface ProjectBorrowerPlanMapperExt extends ProjectBorrowerPlanMapper {
    /**
     * 查询指定期数未还
     * @param projectId
     * @param period
     * @return
     */
    ProjectBorrowerPlan calcRepayAmountPeriod(@Param("borrowerUserId") String borrowerUserId,@Param("projectId") Long projectId, @Param("period") Integer period);


    /**
     * 当期待还本息
     * period
     */
    BigDecimal calcToRepayAmountPeriod(@Param("borrowerUserId") String borrowerUserId,@Param("projectId") Long projectId,@Param("period") Integer period);

    /**
     * 多期待还列表
     * @param projectId
     * @param periods
     * @return
     */
    List<ProjectBorrowerPlan> calcToRepayAmountByPeriods(@Param("projectId") Long projectId,@Param("periods") List<Integer> periods);

    /**
     * 还款计划
     * @param projectId
     * @return
     */
    List<ProjectBorrowerPlan> queryPlanByProjectId(@Param("projectId") Long projectId);
    /**
     *
     * @param projectId
     * @param periods
     * @return
     */
    BigDecimal calcToRepayCipalUnfinishedByPeriods(@Param("projectId") Long projectId,@Param("periods") List<Integer> periods);
    /**
     * 全部待还列表
     *
     */
    List<ProjectBorrowerPlan> calcToRepayAmountTotal(@Param("userId") String userId,@Param("projectId") Long projectId);

    /**
     * 全部罚息未还列表
     *
     */
    List<ProjectBorrowerPlan> calcToRepayPenaltyFee(@Param("userId") String userId,@Param("projectId") Long projectId);


    /**
     * 全部计划列表
     *
     */
    List<ProjectBorrowerPlan> getAllPlanList(@Param("projectId") Long projectId);

    /**
     * 查询总金额
     *
     */
    ProjectSummaryAmountDTO getProjectSummaryAmount(@Param("projectId") Long projectId);

    /**
     *
     * 指定期数时统计当期
     * 期数=null时全部未还期
     * @param projectId
     * @return
     */
    ToRepayTotalAmountDTO queryToRepayTotalAmount(@Param("projectId") Long projectId,@Param("currentPeriod")Integer currentPeriod);

    /**
     * 查询标的累计费用
     * @param projectId
     * @return
     */
    ToRepayTotalAmountDTO queryTotalFeeByProjectId(@Param("projectId") Long projectId);

    /**
     *
     * 查询最大的逾期期数(多期逾期情况下)
     * @param projectId
     * @return
     */
    Integer queryMaxOverduePeriod(@Param("projectId") Long projectId,@Param("currentDate")Date currentDate);

    /**
     * 查询最大的逾期期数(多期逾期情况下)
     *
     * @param projectId
     * @return
     */
    ProjectBorrowerPlan queryCurrentBorrowerPlan(@Param("projectId") Long projectId, @Param("currentDate") Date currentDate);


    /**
     * 查询在上线前或上线后逾期超过一定天数的用户的标的数量-正常还款
     *
     * @param userId
     * @param publishTime
     * @param isBeforePublish
     * @return
     */
    int selectOverdueInConditionForNormalRepayment(@Param("userId") String userId,  @Param("publishTime") String publishTime, @Param("isBeforePublish") int isBeforePublish);

    /**
     * 查询在上线前或上线后逾期超过一定天数的用户的标的数量-逾期还款
     *
     * @param userId
     * @param exceedDays
     * @param publishTime
     * @param isBeforePublish
     * @return
     */
    int selectOverdueInConditionForOverdueRepayment(@Param("userId") String userId, @Param("exceedDays") Integer exceedDays, @Param("publishTime") String publishTime, @Param("isBeforePublish") int isBeforePublish);

    /**
     * 查询在上线前或上线后逾期超过一定天数的用户的标的数量-未还款
     *
     * @param userId
     * @param exceedDays
     * @param publishTime
     * @param isBeforePublish
     * @return
     */
    int selectOverdueInConditionForNonRepayment(@Param("userId") String userId, @Param("exceedDays") Integer exceedDays, @Param("publishTime") String publishTime, @Param("isBeforePublish") int isBeforePublish);

    /**
     * 查询用户的标是不是在逾期中
     * @param projectId
     * @param userId
     * @return
     */
    ProjectBorrowerPlan queryOverdueProjectById(@Param("projectId") Long projectId,@Param("userId") String userId);

    /**
     * 查询愈期超过一定天数的标的-已逾期还款
     * @param userId 用户ID
     * @param exceedDays 愈期天数
     * @return
     */
    Integer selectOverdueExceedConditionRepayment(@Param("userId")String userId,@Param("exceedDays")Integer exceedDays);

    /**
     * 查询愈期超过一定天数的标的-未还款
     * @param userId 用户ID
     * @param exceedDays 愈期天数
     * @return
     */
    Integer selectOverdueExceedConditionNonRepayment(@Param("userId")String userId,@Param("exceedDays")Integer exceedDays);

    /**
     * 查询当前逾期标的数
     * @param userId
     * @return
     */
    int countOverdueProject(@Param("userId") String userId);

    /**
     *  查询应还款金额
     * @param userId
     * @param yearMouth
     * @return
     */
    BigDecimal selectShouldRefundAmount(@Param("borrowerUserId") String userId,@Param("yearMouth")  String yearMouth,@Param("projectId") Long projectId);

    /**
     * 获取大于指定期数的利息之和
     * @param userId
     * @param currentPeriod
     * @param projectId
     * @return
     */
    BigDecimal selectAllNextInterest(@Param("borrowerUserId") String userId,@Param("currentPeriod")  Byte currentPeriod,@Param("projectId") Long projectId );


    /**
     * 查询已还款总额
     * @param userId
     * @return
     */
    BigDecimal selectRepayedAmountTotal(String userId);


    /**
     * 查询用户所有还款标的
     * @param userId
     * @return
     */
    List<CurrMonthRefundPlanDTO> selectAllRefundProjects(@Param("userId") String userId);


    /**
     * 查询用户已结清标的金额
     * @param userId
     * @return
     */
    BigDecimal selectRefund(@Param("userId") String userId);


    /**
     * 查询逾期还款
     * @param userId
     * @param subStatus
     * @return
     */
    List<DelinquencyDTO> selectDelinquency(@Param("userId") String userId, @Param("subStatus") List<Integer> subStatus);


    /**
     * 查询应还款标的
     * @param userId
     * @return
     */
    List<CurrMonthRefundPlanDTO> selectShouldRefundProjects(@Param("userId") String userId);

    /**
     * 查询应还款标的,包含未到期的
     * @param userId 用户ID
     * @return 当前的标
     */
    List<CurrMonthRefundPlanDTO> selectShouldRefundProjectsWithPreData(@Param("userId") String userId);


    /**
     * 查询 还款计划-按日查看 总记录数
     * @param userId
     * @return
     */
    int selectLoanPlanDateCount(String userId);

    /**
     * 还款计划-按日查看
     * @param userId
     * @param start
     * @param end
     * @return
     */
    List<LoanPlanDateResponseDTO> selectLoanPlanDates(@Param("userId") String userId, @Param("start") Integer start, @Param("end") Integer end);


    /**
     * 查询 还款计划-按月查看 总记录数
     * @param userId
     * @return
     */
    int selectLoanPlanMonthCount(String userId);


    /**
     * 还款计划-按月查看
     * @param userId
     * @param start
     * @param end
     * @return
     */
    List<LoanPlanMonthResponseDTO> selectLoanPlanMonths(@Param("userId") String userId, @Param("start") Integer start, @Param("end") Integer end);

    /**
     * 我的借款-进行中 总记录数
     * @param userId
     * @return
     */
    int selectMyloanGoingCount(String userId);


    /**
     * 我的借款-进行中
     * @param userId
     * @return
     */
    List<LoanGoingResponseDTO> selectMyloanGoings(@Param("userId") String userId, @Param("start") Integer start, @Param("end") Integer end);

    /**
     * 我的借款-进行中，根据标ID，期数获取指定的信息
     * @param projectId
     * @param period
     * @return
     */
    LoanGoingResponseDTO selectMyloanGoingsByProjectIdAndPeriod(@Param("projectId") Long projectId,@Param("period") Integer period);

    /**
     * 通过标的ID和用户ID查询还款计划列表
     *
     * @param projectId
     * @param borrowerUserId
     *            (分库分表路由字段)
     * @return
     */
    List<ProjectBorrowerPlan> queryProjectBorrowerPlanListPeriodAscByProjectIdAndBorrowerUserId(
            @Param("projectId") Long projectId, @Param("borrowerUserId") String borrowerUserId);


    /**
     * 逾期笔数
     * @param userId
     * @return
     */
    Integer selectOverdueCount(@Param("userId")String userId, @Param("nowDate")Date nowDate);

    int updateByPrimaryKeyCallBackSelective(ProjectBorrowerPlan projectBorrowerPlan);

    /**
     * 根据项目id和当期期数查询还款计划
     * @param projectId
     * @param currentPeriod
     * @return
     */

    List<ProjectBorrowerPlan> selectBorrowerPlanByProjectIdAndCurrentPeriodBefore(@Param("projectId") Long projectId, @Param("currentPeriod") Integer currentPeriod);

    ProjectBorrowerPlan selectBorrowerPlanByProjectIdAndCurrentPeriod(@Param("projectId") Long projectId, @Param("currentPeriod") Integer currentPeriod);
    List<ProjectBorrowerPlan> selectBorrowerPlanByProjectIdNotRefund(@Param("userId") String userId,@Param("projectId") Long projectId);
    List<ProjectBorrowerPlan> selectBorrowerPlanByProjectIdNotRefundProjectId(@Param("projectId") Long projectId);

    List<ProjectBorrowerPlanDTO> queryMyLoanPlanByToday(@Param("userId") String userId,@Param("today") String today);

    Integer countOverdueNums(@Param("userId") String userId,@Param("projectId") Long projectId);

    /**
     * 查询放款生成的未还款的还款计划
     * @param projectId
     * @return
     */
    List<ProjectBorrowerPlan> selectNotRefund(Long projectId);

    ProjectBorrowerAmountSumDTO sumProjectBorrowerPlanAmountByProjectIdAndBorrowerUserId(@Param("projectId") Long projectId, @Param("borrowerUserId") String userId);

    List<ProjectrePaymentResponseDTO> queryListPeriodAscByProjectIdAndBorrowerUserId(@Param("projectId") Long projectId, @Param("borrowerUserId") String userId);
    /**
     * 查询项目大于等于某期的还款计划列表
     * @param projectId
     * @param currentPeriod
     * @return
     */
    List<ProjectBorrowerPlan> selectByProjectIdAndPeriodsList(@Param("projectId") Long projectId, @Param("currentPeriod") Integer currentPeriod);

    /**
     * 查询项目某期的还款计划
     * @param projectId
     * @param currentPeriod
     * @return
     */
    ProjectBorrowerPlan selectByProjectIdAndPeriodsForUpdate(@Param("projectId") Long projectId, @Param("currentPeriod")Integer currentPeriod);

    /**
     * 查询项目某期的还款计划s
     * @param projectId
     * @param currentPeriod
     * @return
     */
    ProjectBorrowerPlan selectByProjectIdAndPeriods(@Param("projectId") Long projectId, @Param("currentPeriod")Integer currentPeriod);
    /**
     * 查询当天是否进行过还款操作
     * @param beginDate
     * @param endDate
     * @param projectId
     * @return
     */
    int todayRefund(@Param("beginDate")Date beginDate,@Param("endDate")Date endDate,@Param("projectId")Long projectId );

    /**
     * 查询预期未还款计划
     * @param userId
     * @param projectId
     * @param nowDate
     * @return
     */
    List<ProjectBorrowerPlan> selectOverdueNoRepayedPlan(@Param("userId")String userId, @Param("projectId") Long projectId, @Param("nowDate")Date nowDate);

    /**
     * 查询标的总利息
     * @param projectId
     * @return
     */
    BigDecimal selectTotalInterestByProjectId(@Param("projectId") Long projectId);

    /**
     * 查询标的待还金额项
     * @param projectId
     * @return
     */
    CurrentPeriodToReturnDTO selectCurrentPeriodToReturnByProjectId(@Param("projectId")Long projectId);

    /**
     * 查询标的待还罚息
     * @param projectId
     * @return
     */
    BigDecimal selectToRepayPenaltyFeeByProjectId(@Param("projectId") Long projectId);
    /**
     * 查询用户存在逾期的借款
     * @param borrowerUserId
     * @return
     */
    Integer findUserOverProject(String borrowerUserId);


    /**
     * 查询用户标的待还总金额
     * @param projectId
     * @return
     */
    BigDecimal queryShouldRepayAmount(Long projectId);


    /**
     * 查询用户未还期数
     * @param userId
     * @return
     */
    List<Integer> queryShouldRepayPeriods(String userId);

    /**
     * 查询已还最后期数
     * @param projectId
     * @return
     */
    Integer queryLastFinishPeriod(@Param("projectId")Long projectId);
    
    /**
     * 查询历史最大的逾期天数
     * 
     * @param projectId
     * @param currentDate
     * @return
     */
    Integer selectHistoryMaxOverdueDay(@Param("projectId") Long projectId, @Param("currentDate") Date currentDate);

    /**
     * 查询未还款本金总额
     * @param userId
     * @return
     */
    BigDecimal selectNoRepayedRefundedCapital(@Param("userId") String userId);

    /**
     * 查询用户待还本金
     *
     * @param userId
     * @return
     */
    BigDecimal selectUserToPayCapital(@Param("userId")String userId);

    List<ProjectBorrowerPlan> getPlanPageByProjectId(ProjectBorrowerPlanRequestDTO requestDTO);

    Integer getPlanCountByProjectId(@Param("projectId") Long projectId);
}
